package com.quo.controller;

import java.util.List;


import java.util.Random;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.quo.entity.Dept;
import com.quo.entity.Emp;
import com.quo.entity.EmpDto;
import com.quo.entity.EmpDto2;
import com.quo.entity.EmpDto3;
import com.quo.entity.EmpDto4;
import com.quo.entity.EmpLogin;
import com.quo.entity.ProductSeries;
import com.quo.entity.ProductSeries2;
import com.quo.entity.ResetNewPwd;
import com.quo.entity.ResetPersonPwd;
import com.quo.entity.Role;
import com.quo.entity.UpdateUser;
import com.quo.exceptions.LoginException;
import com.quo.mapper.RoleMapper;
import com.quo.service.DeptService;
import com.quo.service.EmpService;
import com.quo.service.ProductSeriesService;
import com.quo.service.RoleService;

import com.quo.service.UserService;
import com.quo.utils.Const;
import com.quo.utils.MD5;
import com.quo.utils.Result;
import com.quo.utils.ResultCode;

/**
 * 
 * @author 韩宛廷
 *
 * 
 */
@RestController("userController")
@RequestMapping(value = "/api")
public class UserController {
	@Autowired
	public UserService uService;
	

	@Autowired
	public EmpService eService;

	// 添加新用户
	@RequestMapping(value = "/user", method = RequestMethod.POST)
	public Result save(@RequestBody EmpDto emp) {
		uService.save(emp);
		System.out.println(emp);
		return new Result(ResultCode.SUCCESS);
	}

	// 获取所有用户信息
	@RequestMapping(value = "/users", method = RequestMethod.GET)
	public Result findEmpAll() {
		List<EmpDto3> elist = uService.getEmpAll();
		Result result = new Result(ResultCode.SUCCESS);
		System.out.println(elist);
		result.setData(elist);
		return result;
	}

	// 获取单个用户信息
	@RequestMapping(value = "/user/{eno}", method = RequestMethod.GET)
	public Result findByEno(@PathVariable(value = "eno") int eno) {
		EmpDto4 emp = uService.getByEno(eno);
		Result result = new Result(ResultCode.SUCCESS);
		result.setData(emp);
		return result;
	}

	// 更新用户信息
	@RequestMapping(value = "/user/{eno}", method = RequestMethod.PUT)
	public Result update(@PathVariable(value = "eno") int eno, @RequestBody EmpDto empdto) {
		// 业务操作
		System.out.println(empdto);
		empdto.setEno(eno);
		uService.update(empdto);
		return new Result(ResultCode.SUCCESS);
	}

	// 重置密码
	@RequestMapping(value = "/user-reset/{eno}", method = RequestMethod.PUT)
	public Result updatePwd(@PathVariable(value = "eno") int eno) throws LoginException {
		// 业务操作
		String pwd = uService.getPwdByEno(eno).getPwd();
		if (pwd.equals("670b14728ad9902aecba32e22fa4f6bd")) {
			// throw new LoginException("是初始用户");
			return new Result(ResultCode.NOFIRSTUSER);
		} else {
			uService.changeEmpPwd(eno);
			return new Result(ResultCode.SUCCESS);
		}

	}

	// 删除单个用户
	@RequestMapping(value = "/user/{eno}", method = RequestMethod.DELETE)
	public Result delete(@PathVariable(value = "eno") int eno) {
		uService.deleteByEno(eno);
		return new Result(ResultCode.SUCCESS);
	}

	// 删除多个用户
	@RequestMapping(value = "/users-del", method = RequestMethod.POST)
	@ResponseBody
	public Result deleteByEnos(@RequestBody int[] enos) {
		uService.deleteByEnos(enos);
		return new Result(ResultCode.SUCCESS);

	}

	// 获取特定个人情报信息
	@RequestMapping(value = "/person", method = RequestMethod.GET)
	public Result findPersonByEno(@CookieValue(name = "a", required = false) String enostr,@CookieValue(name = "b", required = false) String pwd) {
		Result result = new Result();
		int eno;
		try {
			eno = Integer.parseInt(enostr);
		} catch (NumberFormatException n) {
			result = new Result(ResultCode.FAIL);
			result.setMessage("IDまたはパスワードが正しくありません。");
			return result;
		}
		
		if (eno != 0 && pwd != null) {
			try {
				EmpLogin emplogin = eService.login(eno, pwd);
				result = new Result(ResultCode.SUCCESS);
			} catch (LoginException e) {
				result = new Result(ResultCode.FAIL);
				result.setMessage("ログインしていません。");
				return result;
			}
		}

		EmpDto3 emp = uService.getPersonByEno(eno);
		result = new Result(ResultCode.SUCCESS);
		result.setData(emp);
		return result;
	}

	// 更新个人情报信息
	@RequestMapping(value = "/person", method = RequestMethod.PUT)
	public Result updatePerson(@CookieValue(name = "a", required = false) String enostr,@CookieValue(name = "b", required = false) String pwd, @RequestBody UpdateUser user) {
		// 业务操作
		Result result = new Result();
		int eno;
		try {
			eno = Integer.parseInt(enostr);
		} catch (NumberFormatException n) {
			result = new Result(ResultCode.FAIL);
			result.setMessage("IDまたはパスワードが正しくありません。");
			return result;
		}
		
		if (eno != 0 && pwd != null) {
			try {
				EmpLogin emplogin = eService.login(eno, pwd);
				result = new Result(ResultCode.SUCCESS);
			} catch (LoginException e) {
				result = new Result(ResultCode.FAIL);
				result.setMessage("ログインしていません。");
				return result;
			}
		}
		System.out.println(user);
		user.setEno(eno);
		uService.updatePerson(user);
		return new Result(ResultCode.SUCCESS);
	}

	@RequestMapping(value = "/pwdreset", method = RequestMethod.POST)
	@ResponseBody
	public Result pwdreset(@CookieValue(name = "a", required = false) String enostr,@CookieValue(name = "b", required = false) String pwd, @RequestBody ResetPersonPwd rpd)
			throws LoginException {
		Result result = new Result();
		int eno;
		try {
			eno = Integer.parseInt(enostr);
		} catch (NumberFormatException n) {
			result = new Result(ResultCode.FAIL);
			result.setMessage("IDまたはパスワードが正しくありません。");
			return result;
		}
		
		if (eno != 0 && pwd != null) {
			try {
				EmpLogin emplogin = eService.login(eno, pwd);
				result = new Result(ResultCode.SUCCESS);
			} catch (LoginException e) {
				result = new Result(ResultCode.FAIL);
				result.setMessage("ログインしていません。");
				return result;
			}
		}
		rpd.setEno(eno);
		String pwd1 = uService.getPwdByEno(eno).getPwd();
		String pwd2 = rpd.getPwd();
		String oldPWD = MD5.get(pwd2);
		if (pwd1.equals(oldPWD)) {
			String newpwd = rpd.getNewpwd();
			String newPWD = MD5.get(newpwd);
			rpd.setNewpwd(newPWD);
			System.out.println(rpd);
			uService.changePersonPwd(rpd);
		} else {
			return new Result(ResultCode.PWD_ERROR);
		}

		return new Result(ResultCode.SUCCESS);

	}
}
